home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 510 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is this ok: *pointer++ = value ??
  5. Date: Sat, 06 Jan 1996 14:13:37 GMT
  6. Organization: Netcom
  7. Message-ID: <30ee81fc.41548928@nntp.ix.netcom.com>
  8. References: <4cklvv$nmm@alcor.usc.edu>
  9. NNTP-Posting-Host: ix-dc13-29.ix.netcom.com
  10. X-NETCOM-Date: Sat Jan 06  6:13:27 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. wawda@alcor.usc.edu (Abu Wawda) wrote:
  14.  
  15. |>I tried the following on my C compiler and it worked as expected:
  16. |>
  17. |>    int i;
  18. |>    char *pointer;
  19. |>    
  20. |>    pointer = (char *) malloc(50);
  21. |>    for (i=0; i<50; i++) *pointer++ = i;
  22. |>
  23. |>My only problem is why? I mean you aren't allowed to do:
  24. |>
  25. |>    int i,p;
  26. |>
  27. |>    for (i=0; i<50; i++) p++ = i;
  28. |>
  29. |>Like you aren't supposed to be put anything like x+5 or some other
  30. |>unsolved quantity on the left side for assignment. However, is this
  31. an
  32. |>exception for pointers? I can understand doing it backwards as
  33. |>perfectly legal:
  34. |>
  35. |>    int x;
  36. |>
  37. |>    x = *pointer++;
  38. |>
  39. |>but not:
  40. |>
  41. |>    *pointer++ = x;
  42. |>
  43. |>I would appreciate any hints. Thanks you.
  44.  
  45. The problem is that you're thinking in terms of "you aren't supposed
  46. to be put anything like x+5 or some other unsolved quantity on the
  47. left side for assignment."  This rule is too imprecise to work with in
  48. programming.
  49.  
  50. There are specific rules for what may be on the left hand side of an
  51. assignment.  The left operand must be a modifiable lvalue. If p is a
  52. pointer to something that is not const, *p is a modifiable lvalue.
  53. Since pointer is  a pointer to int, pointer++ is also a pointer to int
  54. and *(pointer++) (which is equivalent to *pointer++) is a modifiable
  55. lvalue.
  56.  
  57. Michael M Rubenstein
  58.